/**
 * @author Mr
 *
 */
package com.cz.nongyetongv5.custom.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cz.nongyetongv5.R;

import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.utils.CharsetUtil;

@SuppressLint("NewApi")
public class CustomDialog extends Dialog {

	// 上下文对象
	private Context context = null;

	// 布局管理器
	private LayoutInflater inflater = null;

	// 屏幕尺度对象
	private Display display = null;

	// 确定的文本
	private String actionTxt = null;

	// 取消的文本
	private String cancleTxt = null;

	// 确定单机的时候的事件
	private android.view.View.OnClickListener onActionClickListener;

	// 取消单机的时候的事件
	private android.view.View.OnClickListener onCancleClickListener;

	// 根View
	private View root = null;

	// 底部布局
	private ViewGroup bottomBar = null;

	/**
	 * 内容布局
	 */
	private LinearLayout contentLayout = null;

	// 消息
	private String message = null;

	private TextView tvMessage = null;

	// 标题
	private String title = null;

	private ViewGroup titleLayout = null;

	/**
	 * 宽度的百分比
	 */
	private float heightPercent = 0.3F;

	// 宽度比
	private float widthPercent = 0.8F;

	private String note = "";

	private TextView tvNote = null;

	public CustomDialog(Context context) {

		super(context, R.style.MyDialog);

		this.context = context;

		inflater = LayoutInflater.from(context);

		// 获得显示对象
		display = getWindow().getWindowManager().getDefaultDisplay();

		// 加载根布局
		root = inflater.inflate(R.layout.dialog_template, null);

		setContentView(root);

		setCanceledOnTouchOutside(false);

		// 加载底部布局
		bottomBar = (ViewGroup) root.findViewById(R.id.llBottomBar);

		tvMessage = (TextView) root.findViewById(R.id.tvMessage);

		titleLayout = (ViewGroup) root.findViewById(R.id.llTitleLayout);

		// 清空所有的视图
		bottomBar.removeAllViews();

		// 查询出内容布局
		contentLayout = (LinearLayout) findViewById(R.id.llDialogContentLayout);

		tvNote = (TextView) root.findViewById(R.id.tvNote);
	}

	@Override
	public void setTitle(CharSequence title) {

		if (CharsetUtil.isNotNull((String) title)) {

			this.title = title.toString();

			TextView tvTitle = (TextView) titleLayout.getChildAt(0);

			tvTitle.setText(this.title);

		} else {
			titleLayout.setVisibility(View.GONE);
		}

	}

	@Override
	public void show() {
		super.show();

		android.view.WindowManager.LayoutParams params = getWindow()
				.getAttributes();

		// params.width = (int) ((display.getWidth() ) * widthPercent);
		params.width = LayoutParams.WRAP_CONTENT;

		params.height = (int) (display.getHeight() * heightPercent);

		getWindow().setAttributes(params);

	}

	/**
	 * 显示等待矿
	 */
	public void showWait() {

		super.show();

		android.view.WindowManager.LayoutParams params = getWindow()
				.getAttributes();

		params.width = (int) ((display.getWidth()) * widthPercent);

		// params.width = LayoutParams.WRAP_CONTENT ;

		params.height = (int) (display.getHeight() * heightPercent);

		getWindow().setAttributes(params);

		// 让对话框没有阴影
		// this.getWindow().setDimAmount(0) ;

		// 清除对话框的上下标题和按钮
		this.setTitle(null);

		this.setActionTxt(null);

		this.setCancleTxt(null);

		// 清空所有的视图
		contentLayout.removeAllViews();

		// 清除上边的分隔
		View sp = findViewById(R.id.sp);
		sp.setVisibility(View.GONE);

		contentLayout
				.setBackgroundResource(R.drawable.loadding_dialog_background);

		contentLayout.setAlpha(0.7F);

		// 设置root背景
		root.setBackgroundColor(Color.TRANSPARENT);

		contentLayout.setGravity(Gravity.CENTER);
		inflater.inflate(R.layout.loading_dialog, contentLayout, true);

		// 等待框的内容
		TextView tvWaitDialogTxt = (TextView) contentLayout
				.findViewById(R.id.tvWaitDialogTxt);

		int padding = Utils.dip2px(context, 10);

		tvWaitDialogTxt.setPadding(padding, 0, padding, 0);

		tvWaitDialogTxt.setText(this.message);

	}

	@Override
	public void onBackPressed() {
	}

	public void setView(View view) {

		contentLayout.removeAllViews();

		contentLayout.addView(view, 0, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	public void setActionTxt(String actionTxt) {
		this.actionTxt = actionTxt;

		// 如果取消按钮为空则表示要只显示确定按钮
		if (!CharsetUtil.isNotNull(this.cancleTxt)
				&& CharsetUtil.isNotNull(this.actionTxt)) {

			// 什么都不显示
			bottomBar.removeAllViews();

			// 水平分隔条
			View horizontalSeparator = new View(context);

			horizontalSeparator.setBackgroundColor(Color.parseColor("#C4C3C1"));

			bottomBar.addView(horizontalSeparator, new LayoutParams(
					LayoutParams.MATCH_PARENT, 2));

			// 加载确定按钮
			Button btnAction = new Button(context);

			btnAction.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

			btnAction.setText(this.actionTxt);

			btnAction.setBackgroundResource(R.drawable.btn_back_pressed);

			if (onActionClickListener != null) {
				btnAction.setOnClickListener(onActionClickListener);
			}

			bottomBar.addView(btnAction, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		} else if (CharsetUtil.isNotNull(this.cancleTxt)
				&& CharsetUtil.isNotNull(this.actionTxt)) {

			// 什么都不显示
			bottomBar.removeAllViews();

			// 水平分隔条
			View horizontalSeparator = new View(context);

			horizontalSeparator.setBackgroundColor(Color.parseColor("#C4C3C1"));

			bottomBar.addView(horizontalSeparator, new LayoutParams(
					LayoutParams.MATCH_PARENT, 2));

			// 设置底部布局
			LinearLayout bottomLayout = new LinearLayout(context);
			bottomLayout.setOrientation(LinearLayout.HORIZONTAL);

			// 加载确定按钮
			Button btnAction = new Button(context);

			btnAction.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

			btnAction.setText(this.actionTxt);

			btnAction.setBackgroundResource(R.drawable.btn_back_pressed);

			if (onActionClickListener != null) {
				btnAction.setOnClickListener(onActionClickListener);
			}

			bottomLayout.addView(btnAction, new LinearLayout.LayoutParams(0,
					LayoutParams.WRAP_CONTENT, 1));

			// 加载垂直分割线
			View vierticalSeparator = new View(context);

			vierticalSeparator.setBackgroundColor(Color.parseColor("#C4C3C1"));

			bottomLayout.addView(vierticalSeparator, new LayoutParams(2,
					LayoutParams.MATCH_PARENT));

			// 加载取消按钮
			Button btnCancle = new Button(context);

			btnCancle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

			btnCancle.setText(this.cancleTxt);
			btnCancle.setBackgroundResource(R.drawable.btn_back_pressed);

			if (onCancleClickListener != null) {
				btnCancle.setOnClickListener(onCancleClickListener);
			}

			bottomLayout.addView(btnCancle, new LinearLayout.LayoutParams(0,
					LayoutParams.WRAP_CONTENT, 1));

			bottomBar.addView(bottomLayout, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		} else {
			// 什么都不显示
			bottomBar.removeAllViews();

			bottomBar.setVisibility(View.GONE);
		}

		if (CharsetUtil.isNotNull(this.note)) {

			tvNote.setText(this.note);
			tvNote.setVisibility(View.VISIBLE);

		} else {
			tvNote.setVisibility(View.GONE);

		}

	}

	public void setCancleTxt(String cancleTxt) {
		this.cancleTxt = cancleTxt;
	}

	public void setOnActionClickListener(
			android.view.View.OnClickListener onActionClickListener) {
		this.onActionClickListener = onActionClickListener;
	}

	public void setOnCancleClickListener(
			android.view.View.OnClickListener onCancleClickListener) {
		this.onCancleClickListener = onCancleClickListener;
	}

	// 设置消息
	public void setMessage(String message) {
		this.message = message;

		if (tvMessage != null) {
			tvMessage.setText(this.message);
		}

	}

	// 设置高度的百分比
	public void setHeightPercent(float heightPercent) {
		this.heightPercent = heightPercent;
	}

	// 设置宽度百分比
	public void setWidthPercent(float widthPercent) {
		this.widthPercent = widthPercent;
	}

	public String getActionTxt() {
		return actionTxt;
	}

	public void setNote(String note) {

		this.note = note;
	}
}